import React, { Component } from 'react'
import { connect } from 'react-redux'
import NewTasks from 'graphql/project/download/new.graphql'
import { ValidType } from 'components/validForm'
import {
  Form as FormTable,
  Section as TableSection,
  Switch,
  Submit,
  Input,
} from 'components/tableForm'
import { global as globalDuck } from 'ducks'

const getVariables = (values) => {
  const result = { info: {}, input: {} }
  if (values.description) result.info.description = values.description.value
  if (values.content) result.input.content = values.content.value
  return result
}

class newDataset extends Component {
  componentDidMount = () => {}

  render() {
    const { project } = this.props
    const {
      projectId, projectName, orgName,
    } = project
    const returnUrl = `/project/${orgName}/${projectName}/download`
    const descriptionRule = [
      {
        type: ValidType.MAXLENGTH,
        value: 20,
        errorMessage: '描述过长',
      },
    ]
    return (
      <FormTable>
        <TableSection header="创建下载任务">
          <Input
            nullable
            initValue=""
            name="description"
            header="下载描述"
            description="描述该下载任务(不超过20字符)"
            rules={descriptionRule}
            textArea
          />
          <Switch
            name="content"
            header="下载内容"
            description="选择需要下载的内容"
            options={[{ name: 'all', value: '样本与标注' }, { name: 'sample', value: '只下载样本' }, { name: 'label', value: '只下载标注' }]}
          />
        </TableSection>
        <Submit
          submit={{
            text: '创建',
            variables: { pid: projectId },
            getVariables,
            mutation: NewTasks,
            success: () => returnUrl,
          }}
          cancle={{ text: '取消', url: returnUrl }}
          redirect
        />
      </FormTable>
    )
  }
}

const mapStateToProps = state => ({
  user: globalDuck.selectors.user(state),
})

const mapDispatchToProps = {}

export default connect(
  mapStateToProps,
  mapDispatchToProps,
)(newDataset)
